import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_novel/base/base_list_view.dart';
import 'package:flutter_novel/base/base_stateful_page.dart';
import 'package:flutter_novel/base/empty_widget.dart';
import 'package:flutter_novel/detail/bloc/home_commits_list_bloc.dart';
import 'package:flutter_novel/detail/bloc/home_commits_list_event.dart';
import 'package:flutter_novel/detail/bloc/home_commits_list_state.dart';
import 'package:flutter_novel/detail/widget/home_commits_item.dart';
import 'package:flutter_novel/detail/widget/indication_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HomeCommitsListPage extends BasePage {
  const HomeCommitsListPage({super.key});

  @override
  BasePageState<BasePage> getState() => _HomeCommitsListPageState();
}

class _HomeCommitsListPageState extends BasePageState<HomeCommitsListPage> {
  @override
  void initState() {
    super.initState();
    super.appBarTitle = '评论列表';
    _request();
  }

  Widget _indication() {
    return IndicationWidget();
  }

  Widget _listBody(BuildContext context, HomeCommitsListState state) {
    if (state.list.isEmpty) return EmptyWidget();
    return ListView.builder(
      itemBuilder: (cxt, index) {
        return HomeCommitsItem(model: state.list[index]);
      },
      itemCount: state.list.length,
      padding: EdgeInsets.only(top: 6.h),
    );
  }

  // 请求数据
  void _request() {
    BlocProvider.of<HomeCommitsListBloc>(context).add(GetHomeCommitsListEvent());
  }

  @override
  Widget buildPage(BuildContext context) {
    return BlocBuilder<HomeCommitsListBloc, HomeCommitsListState>(
      builder: (context, state) {
        if (!state.loadFinish) return SizedBox();
        return Stack(children: [_listBody(context, state), _indication()]);
      },
    );
  }
}
